﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using DAL.Artwork;
using DAL.ArtworkLike;
using BLL.ArtworkLike;
using DAL;

namespace BLL.Artwork
{
    public class ArtworkService : BaseServicePro<Models.Artworks>,IArtworkService
    {
        private ArtworkDao artworkDao = new ArtworkDao();
        private ArtworkLikeDao ArtworkLikeDao = new ArtworkLikeDao();

        private ArtworkLikeService artworkLikeService = new ArtworkLikeService();
        /// <summary>
        /// 从数据库删除一条作品
        /// </summary>
        /// <param name="id">作品id</param>
        /// <returns>true为删除成功，反之则删除失败</returns>
        public bool DeleteArtworkById(int id)
        {
            return artworkDao.DeleteArtworkById(id);
        }

        /// <summary>
        /// 通过作品id，获取作品详情信息
        /// </summary>
        /// <param name="id">作品id</param>
        /// <returns>作品实体类</returns>
        public Artworks GetArtworkById(int id)
        {
            return artworkDao.GetArtworkById(id);
        }

        /// <summary>
        /// 获取作品列表
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>作品数组</returns>
        public List<Artworks> GetList(ref int totalCount,int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Artworks> artworks = artworkDao.GetList(ref totalCount, pageIndex, pageSize, orderBy);
            return artworks;
        }

        /// <summary>
        /// 查看该用户下的作品列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>作品数组</returns>
        public List<Artworks> GetList(ref int totalCount, int userId, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt")
        {
            List<Artworks> artworks = artworkDao.GetListByUserId(ref totalCount, userId,pageIndex, pageSize, orderBy);

            return artworks;
        }

        /// <summary>
        /// 通过标签，筛选作品列表
        /// </summary>
        /// <param name="tag">标签</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>作品数组</returns>
        public List<Artworks> GetList(ref int totalCount, string[] tag, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt")
        {
            List<Artworks> artworks = artworkDao.GetListByTag(ref totalCount, tag,pageIndex, pageSize, orderBy);

            return artworks;
        }

        public List<Artworks> GetListOfLiked(ref int totalCount, int owner, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<int> artworkIds = ArtworkLikeDao.GetListByOwnerId(ref totalCount,owner,pageIndex,pageSize,orderBy).Select(it=>it.ArtworkID).ToList();

            List<Models.Artworks> artworks = new List<Artworks>();

            foreach(int id in artworkIds)
            {
                artworks.Add(GetArtworkById(id));
            }

            return artworks;
        }

        public List<Artworks> GetListOfLiked(ref int totalCount, int owner, int likedUid, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Models.Artworks> artworks = GetListOfLiked(ref totalCount,owner,pageIndex,pageSize,orderBy);
            QueryIsLiked(likedUid, artworks);
            return artworks;
        }


        /// <summary>
        /// 获取所有作品数量
        /// </summary>
        /// <returns>作品列表</returns>
        public int GetTotal()
        {
            return artworkDao.GetTotal();
        }

        /// <summary>
        /// 增加作品的浏览热度
        /// </summary>
        /// <param name="artwork">作品实体类</param>
        public void IncrementHot(Artworks artwork)
        {
            artwork.Hot = artwork.Hot + 1;
            UpdateArtwork(artwork);
        }

        /// <summary>
        /// 判断该用户是否点赞该作品
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="artwork">作品实体类</param>
        /// <returns>作品实体类</returns>
        public Artworks QueryIsLiked(int uid, Artworks artwork)
        {
            artwork.IsLike = artworkLikeService.ExistLikeArtwork(artwork.ArtworkID, uid);
            return artwork;
        }

        /// <summary>
        /// 判断该用户是否点赞该作品列表
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="artworks">作品数组</param>
        /// <returns>作品数组</returns>
        public List<Artworks> QueryIsLiked(int uid, List<Artworks> artworks)
        {
            foreach(var item in artworks)
            {
                item.IsLike = artworkLikeService.ExistLikeArtwork(item.ArtworkID, uid);
            }
            return artworks;
        }

        /// <summary>
        /// 保存作品
        /// </summary>
        /// <param name="artwork">作品实体类</param>
        public void SaveArtwork(Artworks artwork)
        {
            artworkDao.InsertArtwork(artwork);
        }

        /// <summary>
        /// 修改作品
        /// </summary>
        /// <param name="artwork">作品实体类</param>
        /// <returns>true为修改成功，反之则失败</returns>
        public bool UpdateArtwork(Artworks artwork)
        {
            artwork.UpdatedAt = DateTime.Now;
            return artworkDao.UpdateArtwork(artwork);
        }

        protected override BaseDaoPro<Artworks> GetDao()
        {
            return new ArtworkDao();
        }
    }
}
